// 定义所有医院相关的接口
const express = require("express")
const router = express.Router();
const Response = require("../utils/Response.js");

// 引入mysql 连接池
const pool = require("../utils/db.js");
const { error } = require("console");

// 查询所有的医院
router.get("/hospital-all", async (req, resp) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize } = req.query;
  // 执行查询数组业务
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from hospital_info limit ?,?";
    let result = await pool.querySync(sql, [startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from hospital_info";
    let result2 = await pool.querySync(sql2, [startIndex, size]);
    let total = result2[0].count;
    resp.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 通过医院名称查询医院
router.post('/hospitalByName',(req,res) =>{
  let {h_name} = req.body
  let sql = 'select * from hospital_info where h_name=?'
  pool.query(sql,[h_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过省份查询该省份下的医院信息
router.post('/hospitalBy-province',(req,res) =>{
  let {h_province} = req.body
  console.log(req.body);
  pool.query('select * from hospital_info where h_province=?',[h_province],(err,result)=>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result));
  })
})

// 通过城市查询该城市下的医院信息
router.post('/hospitalBy-city',(req,res) =>{
  let {h_city} = req.body
  console.log(req.body);
  pool.query('select * from hospital_info where h_city=?',[h_city],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过id查询医院信息
router.get('/hospitalById',(req,res) =>{
  let {h_id} = req.query
  let sql = 'select * from hospital_info where h_id=?'
  pool.query(sql,[h_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 添加医院
router.post('/hospital/add',(req,res) =>{
  let { h_id,h_name,h_cover,h_order,h_province,h_city,h_address,h_introduce,h_phone,h_ordernumber } = req.body;
  let sql = "insert into hospital_info (h_id,h_name,h_cover,h_order,h_province,h_city,h_address,h_introduce,h_phone,h_ordernumber) values (?,?,?,?,?,?,?,?,?,?)";
  pool.query(sql,[h_id,h_name,h_cover,h_order,h_province,h_city,h_address,h_introduce,h_phone,h_ordernumber],(err,result) =>{
    if(err){
      res.send(Response.error(500,error));
      throw err;
    }
    // 将结果返回给客户端
    res.send(Response.ok(result));
  })
})

// 删除医院
router.post('/hospital/delete',(req,res) =>{
  let {h_id} = req.body
  let sql = 'delete from hospital_info where h_id=?'
  pool.query(sql,[h_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 按照医院等级升序排序医院
router.get('/hospital/asc',(req,res) =>{
  let sql = 'select * from hospital_info order by h_ordernumber asc'
  pool.query(sql,(err,result)=>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 按照医院等级降序排序医院
router.get('/hospital/desc',(req,res) =>{
  let sql = 'select * from hospital_info order by h_ordernumber desc'
  pool.query(sql,(err,result)=>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})


// 修改医院
router.post('/hospital/update',(req,res) =>{
  let {h_name,h_cover,h_order,h_province,h_city,h_address,h_introduce,h_phone,h_ordernumber,h_id} = req.body
  let sql = 'update hospital_info set h_name=?,h_cover=?,h_order=?,h_province=?,h_city=?,h_address=?,h_introduce=?,h_phone=?,h_ordernumber=? where h_id=?'
  pool.query(sql,[h_name,h_cover,h_order,h_province,h_city,h_address,h_introduce,h_phone,h_ordernumber,h_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw error
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 将router对象导出
module.exports = router